package com.world.frame;

import com.sun.awt.AWTUtilities;
import com.world.components.MyButton;
import com.world.listener.LoginButtonListener;

import javax.swing.*;
import javax.swing.border.MatteBorder;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;

/**
 * 基础窗口
 *
 * @author WangYX
 * @version 1.0.0
 * @date 2023/11/28 9:45
 */
public class LoginFrame extends JFrame {

    private static final int DEFAULT_WIDTH = 500;

    private static final int DEFAULT_HEIGHT = 500;

    /**
     * 窗口名称
     */
    private static final String DEFAULT_TITLE = "world";

    public LoginFrame() {
        //设置窗口宽高
        this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        //设置标题
        this.setTitle(DEFAULT_TITLE);

        //窗口居中
        Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
        Dimension screenSize = defaultToolkit.getScreenSize();
        setLocation(screenSize.width / 2 - DEFAULT_WIDTH / 2, screenSize.height / 2 - DEFAULT_HEIGHT / 2);
        //图标
        Image image = defaultToolkit.getImage("src/main/resources/img/title.png");
        this.setIconImage(image);

        //布局方式设置为null
        this.getContentPane().setLayout(null);

        //退出操作
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 去掉窗口的装饰
        this.setUndecorated(true);
        //采用指定的窗口装饰风格
        this.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
        //设置窗体圆角，最后两个参数分别为圆角的宽度、高度数值，一般这两个数值都是一样的
        AWTUtilities.setWindowShape(this, new RoundRectangle2D.Double(0.0D, 0.0D, DEFAULT_WIDTH, DEFAULT_HEIGHT, 20.0D, 20.0D));
        //设置背景颜色，记住一定要修改frame.getContentPane()的颜色，因为我们看到的都是这个的颜色而并不是frame的颜色
        this.getContentPane().setBackground(Color.white);

        //插入顶部背景图片
        //创建具有分层的JLayeredPane
        JLayeredPane layeredPane = new JLayeredPane();
        layeredPane.setBounds(0, -5, 500, 150);
        this.getContentPane().add(layeredPane);

        //顶部背景图片
        topBackground(layeredPane);
        //关闭按钮
        closeFunction(layeredPane);
        //最小化功能
        minFunction(layeredPane);

        JTextField userNameField = new JTextField();
        JPasswordField passwordField = new JPasswordField();

        //用户名
        username(userNameField);
        //密码
        password(passwordField);

        MyButton myButton = new MyButton("登录", 0);
        myButton.setBounds(100, 400, 290, 50);
        myButton.addActionListener(new LoginButtonListener(this, userNameField, passwordField));
        this.getContentPane().add(myButton);


    }


    /**
     * 用户名
     */
    private void username(JTextField userNameField) {
        // 创建图片对象
        ImageIcon userNameImg = new ImageIcon();
        userNameImg.setImage(Toolkit.getDefaultToolkit().getImage("src/main/resources/img/username.png"));
        //设置图片在窗体中显示的宽度、高度
        userNameImg.setImage(userNameImg.getImage().getScaledInstance(30, 30, Image.SCALE_DEFAULT));

        //插入用户名输入框前面的图片
        JLabel userNameLabel = new JLabel("");
        userNameLabel.setBounds(100, 220, 30, 30);
        userNameLabel.setIcon(userNameImg);
        //默认获取光标
        userNameLabel.setFocusable(true);
        this.getContentPane().add(userNameLabel);

        //添加圆角的用户名输入框
        MatteBorder bottomBorder = new MatteBorder(0, 0, 1, 0, new Color(192, 192, 192));
        userNameField.setBounds(150, 210, 235, 50);
        userNameField.setBorder(bottomBorder);
        userNameField.setText("用户名");
        userNameField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        //默认设置输入框中的文字颜色为灰色
        userNameField.setForeground(Color.GRAY);
        userNameField.setColumns(10);
        this.getContentPane().add(userNameField);

        userNameField.addFocusListener(new FocusAdapter() {
            //获取光标事件
            @Override
            public void focusGained(FocusEvent e) {
                //获取焦点时，输入框中内容是“用户名”，那么去掉输入框中显示的内容；
                if ("用户名".equals((userNameField.getText().trim()))) {
                    userNameField.setText("");
                    userNameField.setForeground(Color.black);//设置颜色为黑色
                }
            }

            //失去光标事件
            @Override
            public void focusLost(FocusEvent e) {
                //失去焦点时，如果输入框中去掉空格后的字符串为空串则显示用户名
                if ("".equals((userNameField.getText().trim()))) {
                    userNameField.setText("  用户名");
                    userNameField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
                    userNameField.setForeground(Color.GRAY);//默认设置输入框中的文字颜色为灰色
                }
            }
        });
    }

    /**
     * 密码
     */
    private void password(JPasswordField passwordField) {
        // 创建图片对象
        ImageIcon passwordImg = new ImageIcon();
        passwordImg.setImage(Toolkit.getDefaultToolkit().getImage("src/main/resources/img/password.png"));
        //设置图片在窗体中显示的宽度、高度
        passwordImg.setImage(passwordImg.getImage().getScaledInstance(30, 30, Image.SCALE_DEFAULT));
        // 插入密码输入框前面的图片
        JLabel passwordLabel = new JLabel("");
        passwordLabel.setBounds(100, 290, 30, 30);
        passwordLabel.setIcon(passwordImg);
        this.getContentPane().add(passwordLabel);

        MatteBorder bottomBorder = new MatteBorder(0, 0, 1, 0, new Color(192, 192, 192));
        passwordField.setBounds(150, 280, 235, 50);

        passwordField.setBorder(bottomBorder);
        passwordField.setText("密码");
        passwordField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        //默认设置输入框中的文字颜色为灰色
        passwordField.setForeground(Color.GRAY);
        //显示密码输入框中内容
        passwordField.setEchoChar((char) 0);
        this.getContentPane().add(passwordField);

        passwordField.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                //获取焦点时，输入框中内容是“用户名”，那么去掉输入框中显示的内容；
                if ("密码".equals((passwordField.getText().trim()))) {
                    passwordField.setText("");
                    passwordField.setEchoChar('*');//显示密码输入框中内容
                    passwordField.setForeground(Color.black);//设置颜色为黑色
                }

            }

            @Override
            public void focusLost(FocusEvent e) {
                //失去焦点时，如果输入框中去掉空格后的字符串为空串则显示用户名
                if ("".equals((passwordField.getText().trim()))) {
                    passwordField.setText("  密码");
                    passwordField.setFont(new Font("微软雅黑", 0, 14));
                    passwordField.setForeground(Color.GRAY);//默认设置输入框中的文字颜色为灰色
                    passwordField.setEchoChar((char) 0);//显示密码输入框中内容
                }
            }
        });
    }

    /**
     * 顶部图片
     */
    private void topBackground(JLayeredPane layeredPane) {
        // 创建图片对象
        ImageIcon img = new ImageIcon();
        Image image1 = Toolkit.getDefaultToolkit().getImage("src/main/resources/img/world.png");
        img.setImage(image1);
        //设置图片在窗体中显示的宽度、高度
        img.setImage(img.getImage().getScaledInstance(500, 150, Image.SCALE_DEFAULT));

        JPanel panel = new JPanel();
        panel.setBounds(0, -5, 500, 150);
        layeredPane.add(panel, JLayeredPane.DEFAULT_LAYER);

        JLabel lblNewLabel = new JLabel("");
        panel.add(lblNewLabel);
        lblNewLabel.setIcon(img);
    }

    /**
     * 关闭功能
     */
    public void closeFunction(JLayeredPane layeredPane) {
        //插入窗体关闭的背景图片及功能
        ImageIcon closeImg = new ImageIcon();
        Image image2 = Toolkit.getDefaultToolkit().getImage("src/main/resources/img/close.png");
        closeImg.setImage(image2);
        closeImg.setImage(closeImg.getImage().getScaledInstance(25, 25, Image.SCALE_DEFAULT));

        JPanel closePanel = new JPanel();
        //背景设置为透明
        closePanel.setOpaque(false);
        closePanel.setBounds(469, 6, 30, 30);
        layeredPane.add(closePanel, JLayeredPane.MODAL_LAYER);

        JLabel closeLabel = new JLabel("");
        closePanel.add(closeLabel);
        closeLabel.setIcon(closeImg);
        closeLabel.addMouseListener(new MouseAdapter() {
            //鼠标点击关闭图片，实现关闭窗体的功能
            @Override
            public void mouseClicked(MouseEvent e) {
                //dispose();
                System.exit(0);//使用dispose();也可以关闭只是不是真正的关闭
            }
        });
    }

    /**
     * 最小化功能
     */
    public void minFunction(JLayeredPane layeredPane) {
        //插入窗体关闭的背景图片及功能
        ImageIcon minImg = new ImageIcon();
        Image image = Toolkit.getDefaultToolkit().getImage("src/main/resources/img/minus.png");
        minImg.setImage(image);
        minImg.setImage(minImg.getImage().getScaledInstance(25, 25, Image.SCALE_DEFAULT));

        JPanel minPanel = new JPanel();
        minPanel.setOpaque(false);
        minPanel.setBounds(430, 6, 30, 30);
        layeredPane.add(minPanel, JLayeredPane.MODAL_LAYER);

        JLabel minLabel = new JLabel("");
        minPanel.add(minLabel);
        minLabel.setIcon(minImg);
        minLabel.addMouseListener(new MouseAdapter() {
            //鼠标点击最小化图片，实现最小化窗体的功能
            @Override
            public void mouseClicked(MouseEvent e) {
                //最小化窗体
                setExtendedState(JFrame.ICONIFIED);
            }
        });
    }


}
